package 静态方法和实例方法的引用;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[4];
        students[0] = new Student("天",10);
        students[1] = new Student("昊",20);
        students[2] = new Student("李",19);
        students[3] = new Student("吴",12);

//        Arrays.sort(students,new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge() - o2.getAge(); //按年龄升序排序
//            }
//        });

        //使用Lambda简化后的形式
//        Arrays.sort(students,( o1,  o2) -> o1.getAge() - o2.getAge());//按年龄升序排序

        //静态方法调用简化
//        Arrays.sort(students, CompareByDate::compareByAge);//按年龄升序排序

        //实例方法引用
        CompareByDate c = new CompareByDate();
        Arrays.sort(students, c::compareByAgeDesc);//按年龄升序排序

        System.out.println(Arrays.toString(students));

    }
}

class CompareByDate{
    public static int compareByAge(Student s1, Student s2) {
        return s1.getAge() - s2.getAge();
    }
    public int compareByAgeDesc(Student s1, Student s2) {
        return s1.getAge() - s2.getAge();
    }
}

class Student{
    String name;
    int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}